﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace LpbPrj.Client.Tools.Converter
{
    /// <summary>
    /// 公式
    /// </summary>
    public class FormulaMultiConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            double? output = 0;

            try
            {
                if (values != null && values.Count() > 0 && parameter != null)
                {
                    {
                        string param = parameter.ToString();
                        if (string.IsNullOrEmpty(param)) return output;

                        var valuesDouble = values.Where(p => p != DependencyProperty.UnsetValue && p != null).Select(p => System.Convert.ToDouble(p)).ToList();
                        if (valuesDouble.Count() == 2)
                        {
                            if (param.Equals("+"))
                            {
                                output = valuesDouble[0] + valuesDouble[1];
                            }
                            if (param.Equals("-"))
                            {
                                output = valuesDouble[0] - valuesDouble[1];
                            }
                            if (param.Equals("0-"))
                            {
                                output = valuesDouble[0] - valuesDouble[1];
                                output = Math.Max(0, output.Value);
                            }
                            if (param.Equals("*"))
                            {
                                output = valuesDouble[0] * valuesDouble[1];
                            }
                            if (param.Equals("/"))
                            {
                                output = valuesDouble[0] / valuesDouble[1];
                            }
                        }
                        if (valuesDouble.Count() == 3)
                        {
                            if (param.Equals("0/"))
                            {
                                output = Math.Min(valuesDouble[0], valuesDouble[1]) / valuesDouble[2];
                            }
                            if (param.Equals("0*"))
                            {
                                output = Math.Min(valuesDouble[0], valuesDouble[1]) * valuesDouble[2];
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"FormulaMultiConverter, {ex.Message}");
            }
            //System.Diagnostics.Debug.WriteLine($"FormulaMultiConverter, {output}");
            return output;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
